
#include "IWindow.h"
#include "member_BProgressBar.h"

using namespace BWE;

member_BProgressBar::member_BProgressBar(BProgressBar* progressBar)
{
	boss = progressBar;
	ia_align = Align_Center;
	orientation = Orientation_Horizontal;
	value = 0;
	step = 1;
	range.set(0, 100);
	format = "%p%";
}
member_BProgressBar::~member_BProgressBar()
{

}

void member_BProgressBar::freshRects()
{
	BRect crect = boss->centerRect();

	int cx = crect.x();
	int cy = crect.y();
	int cw = crect.width();
	int ch = crect.height();

	int w = cw;
	int h = ch;
	Align align = Align_Left;
	float length = (float)range.size();
	if (orientation & Orientation_Vertical)
	{
		h = length == 0 ? 0 : int((value - range.min()) / length * ch);
		align = Align_BottomCenter;
		if(orientation & Orientation_Reverse)
			align = Align_TopCenter;
	}
	if (orientation & Orientation_Horizontal)
	{
		w = length == 0 ? 0 : int((value - range.min()) / length * cw);
		align = Align_LeftCenter;
		if (orientation & Orientation_Reverse)
			align = Align_RightCenter;
	}

	chunkRect = crect.align(align, BSize(w, h));

	int percent = length > 0 ? int(value / length * 100.0f) : 0;
	BString percentStr = percent;
	BString valueStr = value;
	BString minStr = range.min();
	BString maxStr = range.max();

	text.reset();
	if (format.size())
	{
		text << format;
		if (0 == text.replace("%percent", percentStr))
			text.replace("%p", percentStr);
		if (0 == text.replace("%value", valueStr))
			text.replace("%v", valueStr);
		if (0 == text.replace("%minimum", minStr))
			text.replace("%min", minStr);
		if (0 == text.replace("%maximum", maxStr))
			text.replace("%max", maxStr);
	}

	const BFont* font = boss->font();
	if (font)
	{
		ia_textSize = font->textSize(text);
		this->adjust(crect);
	}

}
